package com.example.demo.pc;

import ch.qos.logback.core.pattern.color.BoldYellowCompositeConverter;
import net.bytebuddy.asm.Advice;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.net.dns.ResolverConfiguration;

import javax.xml.ws.RequestWrapper;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Driver;
import java.util.concurrent.TimeUnit;

import static org.openqa.selenium.WebDriver.*;

/**
 * @author whale_fall
 * @date 2019/4/23
 */

@RequestMapping
@RestController
public class IndexController {

    @RequestMapping(value = "index")
    public String test() {

       /* DesiredCapabilities aDesiredcap = DesiredCapabilities.chrome();
        String nodeUrl="http://192.168.1.101:4444/driver/hub";
        aDesiredcap.setPlatform(Platform.WINDOWS);
       aDesiredcap.setVersion("73.0.3683.103");
        aDesiredcap.setBrowserName("chrome" );
        WebDriver driver = null;
        try {
            driver = new RemoteWebDriver(new URL(nodeUrl),aDesiredcap);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
      //  driver.getWindowHandles();*/
        System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver/chromedriver");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        options.addArguments("--disable-extensions");
        options.addArguments("--disable-gpu");
        options.addArguments("--disable-dev-shm-usage");
        options.addArguments("--no-sandbox");
        WebDriver driver = new ChromeDriver(options);
     //   driver=WebDriver.(chrome_options=chrome_options)
       // Actions action = new Actions(driver);
        String url = "http://www.jikexiu.com/order/selSolution?brandId=8&categoryId=12";
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.get(url);
        new

                WebDriverWait(driver, 15).

                until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".device_id_557")));
        try

        {
            driver.findElement(By.cssSelector(".device_id_557")).click();
            new WebDriverWait(driver, 30).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("ul.center>:nth-child(1)")));
            Thread.sleep(500);
            //  driver.findElement(By.cssSelector("li[deviceattributeid='3043']")).click();
            driver.findElement(By.cssSelector("ul.center>:nth-child(1)")).click();
            Thread.sleep(500);
            driver.findElement(By.cssSelector("ul.Q_one>:nth-child(1)")).click();
            driver.findElement(By.cssSelector(".malfunction_15")).click();
            Thread.sleep(1000);
            driver.findElement(By.cssSelector(".next-page1")).click();
            Thread.sleep(3000);
            driver.switchTo().alert().dismiss();
            new WebDriverWait(driver, 15).until(ExpectedConditions.presenceOfElementLocated(By.id("userName")));
            driver.findElement(By.id("userName")).sendKeys("测试");
            driver.findElement(By.id("mobilePhone")).sendKeys("18323317729");
            new WebDriverWait(driver, 15).until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/section/article/section/article[1]/div[1]/div/form/dl[6]/dd/span[1]")));
            driver.findElement(By.xpath("/html/body/section/article/section/article[1]/div[1]/div/form/dl[6]/dd/span[1]")).click();
            new WebDriverWait(driver, 15).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("ul#cmbProvince>:nth-child(28)")));
            driver.findElement(By.cssSelector("ul#cmbProvince>:nth-child(28)")).click();
            new WebDriverWait(driver, 15).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("ul#cmbCity>:nth-child(1)")));
            driver.findElement(By.cssSelector("ul#cmbCity>:nth-child(1)")).click();
            new WebDriverWait(driver, 15).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("ul#cmbArea>:nth-child(5)")));
            driver.findElement(By.cssSelector("ul#cmbArea>:nth-child(5)")).click();
            driver.findElement(By.id("Address")).sendKeys("测试");
            driver.findElement(By.id("repairMethodDate")).click();
            new WebDriverWait(driver, 15).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("ul#datelay>:nth-child(1)")));
            driver.findElement(By.cssSelector("ul#datelay>:nth-child(1)")).click();
            Thread.sleep(500);
            new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("ul#Stimelay>:nth-child(5)"))));
            //  new WebDriverWait(driver,5).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("ul#Stimelay>:nth-child(5)")));
            Thread.sleep(500);
            driver.findElement(By.cssSelector("ul#Stimelay>:nth-child(5)")).click();
            new WebDriverWait(driver, 15).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("ul#Etimelay>:nth-child(5)")));
            driver.findElement(By.cssSelector("ul#Etimelay>:nth-child(5)")).click();
            driver.findElement(By.id("sumitOrderInfo__")).click();
            Thread.sleep(500);
            String title = driver.getTitle();
            String url2 = driver.getCurrentUrl();
            assert title.equals("订单提交成功");
            String[] a = url2.split("=");
            //  System.out.println("您的订单id为："+a[1]);
            return "您的orderId="+a[1];

        } catch (
                InterruptedException e)

        {
            e.printStackTrace();
        }
        driver.quit();
        return null;
    }

}
